package com.alinesno.cloud.portal.auto.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alinesno.cloud.portal.auto.task.AsyncTaskManager;
import com.alinesno.cloud.portal.auto.task.TaskInfo;

/**
 * 定时任务示例
 * 
 * @author WeiXiaoJin
 * @since 2019年11月22日 上午7:56:00
 */
@RestController
@RequestMapping(value = "/api/v1/asynctask")
public class AsyncTaskController {
	
	// 注入异步任务管理器
	@Autowired
	AsyncTaskManager asyncTaskManager;

	@RequestMapping(value = "/startTask", method = RequestMethod.GET)
	public ResponseEntity<TaskInfo> startAsyncTask() {
		// 调用任务管理器中的submit去提交一个异步任务
		TaskInfo taskInfo = asyncTaskManager.submit(() -> {
			try {
				// 模拟异步，睡眠6秒
				Thread.sleep(30*1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		});
		return ResponseEntity.ok(taskInfo); // RestMessageUtil.objectToRestMessage(taskInfo);
	}

	@RequestMapping(value = "/getTaskStatus", method = RequestMethod.GET)
	public ResponseEntity<TaskInfo> getTaskStatus(@RequestParam("taskId") String taskId) {
		return ResponseEntity.ok(asyncTaskManager.getTaskInfo(taskId));
	}
}